<?php
/**
 * Created by PhpStorm.
 * 订单发票处理
 * User: caicm
 * Date: 2017/9/1
 * Time: 17:56
 */

namespace SDXApi\Model;


use Think\Model;

class OrderInvoiceModel extends Model {

    /**
     * 添加订单发票
     * Created By caicm
     * @param int $order_id
     * @param int $is_supplement
     * @return mixed
     */
    public function add_invoice($order_id = 0, $is_supplement=0){

        $data['version'] = I('post.version');
        $data['source'] = get_source();
        $data['order_id'] = $order_id;
        $data['add_time'] = gmtime();
        $data['invoice_state'] = 1; 	        //状态 1-未处理 2-已处理
        $data['is_supplement'] = $is_supplement;

        $data['invoice_type'] = I('post.invoice_type', 1, 'int'); 	            //发票类型 1-电子发票 2-纸质发票
        $data['invoice_mobile'] = I('post.invoice_mobile'); 	                //手机号
        $data['invoice_email'] = I('post.invoice_email'); 	                    //邮箱
        $data['invoice_title_type'] = I('post.invoice_title_type', 1, 'int'); 	//抬头类型 1-个人 2-单位
        $data['invoice_title_content'] = I('post.invoice_title_content'); 	    //抬头内容（单位名称）
        $data['invoice_identify_number'] = I('post.invoice_identify_number'); 	//纳税人识别号
        $data['invoice_content'] = I('post.invoice_content'); 	                //发票内容
        $data['invoice_remark'] = I('post.invoice_remark'); 	                //用户备注


        return $this->add($data);

    }

    /**
     * 查询订单发票详情
     * Created  By caicm
     * @param int $order_id
     * @return mixed
     */
    public function get_invoice_info($order_id = 0){
        $where = array(
            'order_id' => $order_id
        );

        $field = "
            invoice_state,
            invoice_type,
            invoice_mobile,
            invoice_email,
            invoice_title_type,
            invoice_title_content,
            invoice_identify_number,
            invoice_content,
            invoice_remark,
            invoice_id,
            invoice_img,
            invoice_file
        ";

        $data = $this->where($where)->field($field)->find();
        if($data){
            $data['invoice_file'] = $data['invoice_file'] ? $data['invoice_file'] : '';
        }

        return $data;

    }

    /**
     * 发票内容
     * @return array
     */
    public function get_invoice_content(){
        $data = array('明细');
        return $data;
    }


    public function get_order_invoice($order_id = 0){

        $where = array(
            'o.order_id' => $order_id
        );

        $field =    'o.order_id,
                    o.order_status,
                    o.shipping_status,
                    o.pay_status,
                    i.invoice_id,
                    i.invoice_title_type,
                    i.invoice_title_content,
                    i.invoice_remark,
                    i.invoice_title_type
                    ';

        $join = 'left join sdx_order_invoice as i on o.order_id=i.order_id';

        return M('order_info as o')->where($where)->field($field)->join($join)->find();

    }

    public function get_invoice_file($invoice_id = 0){

        $where = array(
            'i.invoice_id' => $invoice_id
        );

        $field =    'o.order_sn,
                    i.invoice_id,
                    i.invoice_file
                    ';

        $join = 'left join sdx_order_info as o on o.order_id=i.order_id';

        return $this->alias('i')->where($where)->field($field)->join($join)->find();

    }


}